/*
    This file is part of SQLiteGTK, written by Aly Hirani

    SQLiteGTK is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SQLiteGTK is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SQLiteGTK.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "include.h"
#include "main_window_notebook.h"
#include "main_window_notebook_source_view.h"
#include "util.h"

gboolean
main_window_notebook_is_active_page_type(GtkNotebook *notebook, MainWindowNotebookPageType page_type)
{
	gint current_page;
	GtkWidget *active_page;
	
	g_assert(notebook);
	
	current_page = gtk_notebook_get_current_page(notebook);

	if(current_page == -1)
	   return page_type == MWNPT_NONE;
	
	active_page = gtk_notebook_get_nth_page(notebook, current_page);
	g_assert(active_page);
	
	return page_type & GPOINTER_TO_INT(g_object_get_data(G_OBJECT(active_page), "page_type"));
}

gboolean
main_window_notebook_is_nth_page_type(GtkNotebook *notebook, gint page_num, MainWindowNotebookPageType page_type)
{
	GtkWidget *active_page;
	
	g_assert(notebook);
	
	active_page = gtk_notebook_get_nth_page(notebook, page_num);
	g_assert(active_page);
	
	return page_type & GPOINTER_TO_INT(g_object_get_data(G_OBJECT(active_page), "page_type"));
}

MainWindowNotebookPageType main_window_notebook_get_active_page_type(GtkNotebook *notebook)
{
	gint current_page;
	GtkWidget *active_page;
	GObject *page;
	
	g_assert(notebook);
	
	current_page = gtk_notebook_get_current_page(notebook);
	g_assert(current_page >= 0);
	
	active_page = gtk_notebook_get_nth_page(notebook, current_page);
	g_assert(active_page);
	
	page = G_OBJECT(active_page);
	g_assert(page);
	
	return GPOINTER_TO_INT(g_object_get_data(page, "page_type"));
}

MainWindowNotebookPageType main_window_notebook_get_page_type_from_page(GtkWidget *active_page)
{
	GObject *page;
	
	g_assert(active_page);
	
	page = G_OBJECT(active_page);
	g_assert(page);
	
	return GPOINTER_TO_INT(g_object_get_data(page, "page_type"));
}

GtkBuilder *main_window_notebook_get_active_page_builder_from_notebook(GtkNotebook *notebook)
{
	GtkWidget *active_page;
	gint current_page;
	GtkBuilder *builder;
	
	g_assert(notebook);
	
	current_page = gtk_notebook_get_current_page(notebook);
	g_assert(current_page >= 0);
	
	active_page = gtk_notebook_get_nth_page(notebook, current_page);
	g_assert(active_page);
	
	builder = g_object_get_data(G_OBJECT(active_page), "page_builder");
	g_assert(builder);
	
	return builder;
}

GtkBuilder *main_window_notebook_get_active_page_builder_from_builder(GtkBuilder *builder)
{
	gint current_page;
	GtkNotebook *notebook;
	GObject *current_widget;
	GtkBuilder *page_builder;
	
	g_assert(builder);
	
	notebook = GTK_NOTEBOOK(gtk_builder_get_object(builder, "notebook"));
	g_assert(notebook);
	
	current_page = gtk_notebook_get_current_page(notebook);
	g_assert(current_page >= 0);
	
	current_widget = G_OBJECT(gtk_notebook_get_nth_page(notebook, current_page));
	g_assert(current_widget);
	
	page_builder = GTK_BUILDER(g_object_get_data(current_widget, "page_builder"));
	g_assert(page_builder);
	
	return page_builder;
}

GtkBuilder *main_window_notebook_get_active_page_builder_from_user_pointer(gpointer user_data)
{
	return main_window_notebook_get_active_page_builder_from_builder(GTK_BUILDER(user_data));
}

MainWindowNotebookPageType main_window_notebook_get_nth_page_type(GtkNotebook *notebook, gint page_num)
{
	GtkWidget *page;
	
	g_assert(page_num >= 0);
	g_assert(notebook);
	
	page = gtk_notebook_get_nth_page(notebook, page_num);
	g_assert(page);
	
	return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(page), "page_type"));
}

void main_window_notebook_set_active_page_type(GtkNotebook *notebook, MainWindowNotebookPageType page_type)
{
	gint current_page;
	GtkWidget *active_page;
	
	g_assert(notebook);
	
	current_page = gtk_notebook_get_current_page(notebook);
	g_assert(current_page >= 0);
	
	active_page = gtk_notebook_get_nth_page(notebook, current_page);
	g_assert(active_page);
	
	g_object_set_data(G_OBJECT(active_page), "page_type", GINT_TO_POINTER(page_type));
}

void main_window_notebook_set_nth_page_type(GtkNotebook *notebook, gint page_num, MainWindowNotebookPageType page_type)
{
	GtkWidget *page;
	
	g_assert(notebook);
	g_assert(page_num >= 0);
	
	page = gtk_notebook_get_nth_page(notebook, page_num);
	g_assert(page);
	
	g_object_set_data(G_OBJECT(page), "page_type", GINT_TO_POINTER(page_type));
}

void
main_window_notebook_rename_page_label(GtkWidget *notebook_page_label, const gchar *message)
{
	GtkBuilder *header_builder;
	GtkLabel *label;
	
	g_assert(notebook_page_label);
	g_assert(message);
	
	header_builder = GTK_BUILDER(g_object_get_data(G_OBJECT(notebook_page_label), "builder"));
	g_assert(header_builder);
	
	label = GTK_LABEL(gtk_builder_get_object(header_builder, "caption_label"));
	g_assert(label);
	
	gtk_label_set_text(label, message);
}

void
main_window_notebook_rename_page(GtkNotebook *notebook, GtkWidget *notebook_page, const gchar *message)
{
	GtkWidget *notebook_page_label;
	
	g_assert(notebook);
	g_assert(notebook_page);
	g_assert(message);
	
	notebook_page_label = gtk_notebook_get_tab_label(notebook, notebook_page);
	g_assert(notebook_page_label);
	
	main_window_notebook_rename_page_label(notebook_page_label, message);
}

/* FIXME: Header should be a builder as well?! */
void
main_window_notebook_set_page_modified(GtkNotebook *notebook, GtkWidget *notebook_page, gboolean modified)
{
	GtkWidget *header_box;
	GList *children;
	GtkLabel *modified_label;
	
	g_assert(notebook);
	g_assert(notebook_page);
	
	header_box = gtk_notebook_get_tab_label(notebook, notebook_page);
	g_assert(header_box);
	
	children = gtk_container_get_children(GTK_CONTAINER(header_box));
	g_assert(children);
	
	modified_label = GTK_LABEL(g_list_nth_data(children, 1));
	
	gtk_label_set_text(modified_label, (modified ? " *" : " "));
}

/* FIXME: Header should be a GTKBuilder as well?! */
GtkWidget *
main_window_notebook_create_tab_header(const gchar *title, GCallback close_button_clicked_connect, gpointer user_data)
{
	GtkBox *box;
	GtkImage *icon_close_button, *icon;
	GtkLabel *label, *modified_label;
	GtkButton *close_button;
	
	box = GTK_BOX(gtk_hbox_new(FALSE, 0));
	g_assert(box);
	
	icon_close_button = GTK_IMAGE(gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU));
	g_assert(icon_close_button);
	
	icon = GTK_IMAGE(gtk_image_new_from_stock(GTK_STOCK_NEW, GTK_ICON_SIZE_MENU));
	g_assert(icon);
	
	label = GTK_LABEL(gtk_label_new(title));
	g_assert(label);
	
	modified_label = GTK_LABEL(gtk_label_new(" "));
	g_assert(modified_label);
	
	close_button = GTK_BUTTON(gtk_button_new());
	g_assert(close_button);
	
	gtk_button_set_image(close_button, GTK_WIDGET(icon_close_button));
	gtk_button_set_relief(close_button, GTK_RELIEF_NONE);
	g_signal_connect(G_OBJECT(close_button), "clicked", close_button_clicked_connect, user_data);
	
	gtk_box_pack_start(box, GTK_WIDGET(icon), FALSE, FALSE, 0);
	gtk_box_pack_start(box, GTK_WIDGET(modified_label), TRUE, TRUE, 0);
	gtk_box_pack_start(box, GTK_WIDGET(label), TRUE, TRUE, 0);
	gtk_box_pack_end(box, GTK_WIDGET(close_button), FALSE, FALSE, 0);
	
	gtk_widget_show_all(GTK_WIDGET(box));
	
	return GTK_WIDGET(box);
}

gboolean
main_window_notebook_close_all_tabs(GtkNotebook *notebook, gpointer user_data)
{
	GtkBuilder *builder;
	gint i;
	GList *temp_list = NULL;
	
	g_assert(notebook);
	g_assert(user_data);

	builder = GTK_BUILDER(user_data);
	g_assert(builder);

	/* work backwards to make less calls to get_n_pages */

	while((i = gtk_notebook_get_n_pages(notebook)) > 0)
	{
		MainWindowNotebookPageType current_type = MWNPT_NONE;
		for(; i>=0; --i)
		{
			/* hack */
			if(current_type == MWNPT_NONE)
				current_type = main_window_notebook_get_nth_page_type(notebook, i);

			if(main_window_notebook_get_nth_page_type(notebook, i) == current_type)
			{
				temp_list = g_list_prepend(temp_list, gtk_notebook_get_nth_page(notebook, i));
			}
		}

		/* I don't really like this */
		switch(current_type)
		{
			case MWNPT_TABLES_LIST:
				break;
			case MWNPT_EDITOR:
#ifdef ENABLE_SOURCE_VIEW
				if(!main_window_editor_tabs_close(notebook, temp_list))
					return FALSE;
				break;
#endif /* ENABLE_SOURCE_VIEW */
			case MWNPT_TABLE_EDIT:
				/* TODO: Dispatcher here */
				break;
			default:
				g_assert(FALSE);
		}
		g_list_free(temp_list);
		temp_list = NULL;
	}

	return TRUE;
}

void
main_window_notebook_change_current_page_cb(GtkNotebook *notebook, GtkNotebookPage *page, guint page_num, gpointer user_data)
{
	GtkBuilder *builder;
	GtkWidget *active_page;
	MainWindowNotebookPageType page_type;
	
	g_assert(user_data);
	g_assert(page);
	
	builder = GTK_BUILDER(user_data);
	g_assert(builder);
	
	active_page = GTK_WIDGET(page);
	g_assert(active_page);
	
	page_type = main_window_notebook_get_page_type_from_page(active_page);
	
	/* FIXME: Finish this implementation */
	switch(page_type)
	{
	case MWNPT_EDITOR:
		break;
	case MWNPT_TABLE_EDIT:
		break;
	case MWNPT_TABLES_LIST:
		break;
	default:
		log_error("Unknown page type in main_window_notebook_change_current_page_cb");
		break;
	}
	
	/*
	GtkToolButton *editor_toolbar_save, *editor_toolbar_execute, *editor_toolbar_refresh;
	
	g_assert(notebook);
	g_assert(user_data);
	
	builder = GTK_BUILDER(user_data);
	g_assert(builder);
	
	editor_toolbar_save = GTK_TOOL_BUTTON(gtk_builder_get_object(builder, "editor_toolbar_save"));
	g_assert(editor_toolbar_save);
	
	editor_toolbar_execute = GTK_TOOL_BUTTON(gtk_builder_get_object(builder, "editor_toolbar_execute"));
	g_assert(editor_toolbar_execute);
	
	editor_toolbar_refresh = GTK_TOOL_BUTTON(gtk_builder_get_object(builder, "editor_toolbar_refresh"));
	g_assert(editor_toolbar_refresh);
	
	gtk_widget_set_sensitive(GTK_WIDGET(editor_toolbar_save), page_num != 0);
	gtk_widget_set_sensitive(GTK_WIDGET(editor_toolbar_execute), page_num != 0);
	gtk_widget_set_sensitive(GTK_WIDGET(editor_toolbar_refresh), page_num == 0);
	*/
}

GtkNotebook
*main_window_notebook_get_notebook_from_builder(GtkBuilder *builder)
{
	GtkNotebook *notebook;

	g_assert(builder);

	notebook = GTK_NOTEBOOK(gtk_builder_get_object(builder, "notebook"));
	g_assert(notebook);

	return notebook;
}

GtkNotebook
*main_window_notebook_get_notebook_from_user_data(gpointer user_data)
{
	return main_window_notebook_get_notebook_from_builder(GTK_BUILDER(user_data));
}
